package com.zyq.support;

import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public abstract class BaseController {
    protected static final ExecutorService pool = Executors.newFixedThreadPool(100);

    protected Stage getStage() {
        return Context.context.getStage();
    }


    protected Alert showConfirm(String title, String message, Runnable success, Runnable cancel) {
        Alert _alert = new Alert(Alert.AlertType.NONE, message, new ButtonType("取消", ButtonBar.ButtonData.NO),
                new ButtonType("确定", ButtonBar.ButtonData.YES));
        _alert.setTitle(title);
        _alert.initOwner(getStage());
        Optional<ButtonType> _buttonType = _alert.showAndWait();
        if (_buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES)) {
            success.run();
        } else {
            cancel.run();
        }
        return _alert;
    }

    protected void runTask(Task task) {
        pool.execute(task);
    }

    protected void showInfo(String title, String message) {
        Alert _alert = new Alert(Alert.AlertType.WARNING, message);
        _alert.setTitle(title);
        _alert.initOwner(getStage());
        _alert.show();
    }

    protected <T> void bindTask(Property<T> property, ObservableValue<T> observablevalue) {
        if (property.isBound()) property.unbind();
        property.bind(observablevalue);
    }

}
